package com.cloud.jvm.ref;

import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;

/**
 * @version v1.0
 * @author: TianXiang
 * @description: 引用队列
 * @date: 2021/5/31
 */
public class RefQueue {

    public static void main(String[] args) {

        Object o1 = new Object();
        ReferenceQueue queue = new ReferenceQueue();
        WeakReference<Object> weakRef = new WeakReference<>(o1, queue);
        System.out.println(o1);// java.lang.Object@2957fcb0
        System.out.println(weakRef.get());// java.lang.Object@2957fcb0
        // 引用队列在GC前是空的
        System.out.println(queue.poll());// null

        o1 = null;
        System.gc();
        System.out.println("==========================================================");

        System.out.println(o1);// null
        System.out.println(weakRef.get());// null
        // 发生GC时会将引用对象存到队列
        System.out.println(queue);// java.lang.ref.ReferenceQueue@1376c05c


    }




}
